package io.scnb.lesson001.demo13;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LookupMethodTest {

    @Test
    public void normalBean() {
        String beanXml = "classpath:/io/scnb/lesson001/demo13/normalBean.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(beanXml);
        // 每次从容器中获取ServiceA都会得到一个不同的对象
        System.out.println(context.getBean(ServiceA.class));
        System.out.println(context.getBean(ServiceA.class));

        System.out.println("serviceB中的serviceA");
        ServiceB serviceB = context.getBean(ServiceB.class);
        // 每次从serviceB对象中得到的serviceA对象是一样的
        // 因为serviceB是单例的，容器创建ServiceB的时候，会从容器中获取一个ServiceA将其注入到ServiceB中
        System.out.println(serviceB.getServiceA());
        System.out.println(serviceB.getServiceA());
    }

    @Test
    public void applicationcontextaware() {
        String beanXml = "classpath:/io/scnb/lesson001/demo13/applicationcontextaware.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(beanXml);

        System.out.println(context.getBean(io.scnb.lesson001.demo13.applicationcontextaware.ServiceA.class));
        System.out.println(context.getBean(io.scnb.lesson001.demo13.applicationcontextaware.ServiceA.class));

        System.out.println("serviceB中的serviceA");
        io.scnb.lesson001.demo13.applicationcontextaware.ServiceB bean = context.getBean(io.scnb.lesson001.demo13.applicationcontextaware.ServiceB.class);
        bean.say();
        bean.say();
    }

    @Test
    public void lookupmethod() {
        String beanXml = "classpath:/io/scnb/lesson001/demo13/lookupmethod.xml";
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(beanXml);

        System.out.println(context.getBean(io.scnb.lesson001.demo13.lookupmethod.ServiceA.class));
        System.out.println(context.getBean(io.scnb.lesson001.demo13.lookupmethod.ServiceA.class));

        System.out.println("serviceB中的serviceA");
        io.scnb.lesson001.demo13.lookupmethod.ServiceB bean = context.getBean(io.scnb.lesson001.demo13.lookupmethod.ServiceB.class);
        bean.say();
        bean.say();
    }
}
